<template>
  <scroll-view class="viewport" scroll-y enable-back-to-top>
    <!-- 个人资料 -->
    <view class="profile" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
      <!-- 情况1：已登录 -->
      <view class="overview" v-if="memberStore?.token">
        <navigator url="/pagesMember/profile/profile" hover-class="none">
          <image
            class="avatar"
            mode="aspectFill"
            :src="memberStore.userInfo?.avatarUrl"
          ></image>
        </navigator>
        <view class="meta">
          <view class="nickname">{{memberStore.userInfo?.nickName}}</view>
          <navigator class="extra" url="/pagesMember/profile/profile" hover-class="none">
            <text class="update">更新头像昵称</text>
          </navigator>
        </view>
      </view>
      <!-- 情况2：未登录 -->
      <view class="overview" v-else>
        <navigator url="/pages/login/login" hover-class="none">
          <image
            class="avatar gray"
            mode="aspectFill"
            src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png"
          ></image>
        </navigator>
        <view class="meta">
          <view class="extra">
            <text class="tips" @tap="getUserProfile">点击登录账号</text>
          </view>
        </view>
      </view>
      <navigator class="settings" url="/pagesMember/settings/settings" hover-class="none">
        设置
      </navigator>
    </view>
    <!-- 我的订单 -->
    <view class="orders">
      <view class="section">
        <!-- 订单 -->
        <navigator
          v-for="item in orderTypes"
          :key="item.type"
          url="/pages/historyFallRiskSelf/historyFallRiskSelf"
          class="navigator"
          hover-class="none"
        >
          {{ item.text }}
        </navigator>
      </view>
    </view>
    <view class="list_box">
      <view class="viewport_list">
        <view class="list">
          <navigator hover-class="none" class="item arrow" url=" ">个人资料</navigator>
        </view>
        <view class="list">
          <navigator hover-class="none" class="item arrow" url=" ">学习模块</navigator>
        </view>
        <view class="list">
          <navigator hover-class="none" class="item arrow" url=" ">查看跌倒风险报告</navigator>
        </view>
        <view class="list">
          <navigator hover-class="none" class="item arrow" url=" ">查看风险自评表记录</navigator>
        </view>
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import HeadTabar from '@/components/headTabar.vue'
import {ref,reactive} from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import {useMemberStore} from '@/stores/modules/member'
import {wxLoginApi,isLoginApi} from '@/apis/login/loginWX'
import {getUserInfoApi} from '@/apis/user/user'
onLoad(async()=>{
  const res = await wx.login()
  const memberStore = useMemberStore()
  memberStore.setCode(res.code)
  console.log(res.code)
})
const token = ref('')
const memberStore = useMemberStore()
const hasUserInfo = ref(false)
const getUserProfile =async ()=>{
   const isUserBind = await wxLoginApi(memberStore.code)//判断用户是否绑定
   console.log('用户是否绑定:',isUserBind.code)
   if(isUserBind.code==200)//用户绑定了
   {
    memberStore.setToken(isUserBind.data.token);
    console.log('用户已经绑定了的，再次登录返回的token：',memberStore.token)
    const resUserInfo = await getUserInfoApi()
    memberStore.setUserInfo(resUserInfo.data)
    console.log('用户信息:',resUserInfo.data)
   }
  if(isUserBind.code==201)//用户没绑定
  { 
    const res_first=await wx.getUserProfile({desc:'用于临床防跌倒预防实践系统登录'})
    console.log("我是微信返回的用户信息：",res_first)
    memberStore.setUserInfo(res_first?.userInfo)
    const {userInfo} = memberStore
    const res_code =await wx.login()//须生成新的code
    const msg = {
    "code":res_code.code,
    "nickname": userInfo.nickName,
    "avatarUrl":userInfo.avatarUrl,
    "gender":userInfo.gender
  }
    const res_isLogin = await isLoginApi(msg)//将用户信息存到数据库
    token.value = res_isLogin.data?.token
    memberStore.setToken(token.value)
    console.log('用户第一次绑定了的，再次登录返回的token：',memberStore.token)
  }
}
const { safeAreaInsets } = uni.getSystemInfoSync()// 获取屏幕边界到安全区域距离
// 订单选项
const orderTypes = [
  { type: 1, text: '历史风险报告', icon: 'icon-currency' },
  { type: 2, text: '已填写的自评表数量', icon: 'icon-gift' },
  { type: 3, text: '学习进度', icon: 'icon-check' }
]
</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}
.viewport {
  height: 100%;
  background-repeat: no-repeat;
  background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/center_bg.png);
  background-size: 100% auto;
}

/* 用户信息 */
.profile {
  margin-top: 20rpx;
  position: relative;
  .overview {
    display: flex;
    height: 120rpx;
    padding: 0 36rpx;
    color: #fff;
  }
  .avatar {
    width: 120rpx;
    height: 120rpx;
    border-radius: 50%;
    background-color: #eee;
  }
  .gray {
    filter: grayscale(100%);
  }
  .meta {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    line-height: 30rpx;
    padding: 16rpx 0;
    margin-left: 20rpx;
  }
  .nickname {
    max-width: 350rpx;
    margin-bottom: 16rpx;
    font-size: 30rpx;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .extra {
    display: flex;
    font-size: 20rpx;
  }
  .tips {
    font-size: 22rpx;
  }
  .update {
    padding: 3rpx 10rpx 1rpx;
    color: rgba(255, 255, 255, 0.8);
    border: 1rpx solid rgba(255, 255, 255, 0.8);
    margin-right: 10rpx;
    border-radius: 30rpx;
  }
  .settings {
    position: absolute;
    bottom: 0;
    right: 40rpx;
    font-size: 30rpx;
    color: #fff;
  }
}
/* 我的订单 */
.orders {
  position: relative;
  z-index: 99;
  padding: 30rpx;
  margin: 50rpx 20rpx 0;
  background-color: #fff;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);
  height: 150rpx;

  .section {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 40rpx 20rpx 10rpx;
    .navigator,
    .contact {
      text-align: center;
      font-size: 24rpx;
      color: #333;
      &::before {
        display: block;
        font-size: 60rpx;
        color: #ff9545;
      }
    }
    .contact {
      padding: 0;
      margin: 0;
      border: 0;
      background-color: transparent;
      line-height: inherit;
    }
  }
}
  .list_box{
  margin-top: 20rpx;
  .viewport_list {
  padding: 20rpx;
}
  
/* 列表 */
.list {
  padding: 0 20rpx;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
  .item {
    line-height: 90rpx;
    padding-left: 10rpx;
    font-size: 30rpx;
    color: #333;
    border-top: 1rpx solid #ddd;
    position: relative;
    text-align: left;
    border-radius: 0;
    background-color: #fff;
    &::after {
      width: auto;
      height: auto;
      left: auto;
      border: none;
    }
    &:first-child {
      border: none;
    }
    &::after {
      right: 5rpx;
    }
  }
  .arrow::after {
    content: '\e600';
    position: absolute;
    top: 50%;
    color: #ccc;
    font-family: 'erabbit' !important;
    font-size: 32rpx;
    transform: translateY(-50%);
  }
}
}

</style>